package id.net.iconpln.bb.client.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.BeanModelDetailModul;
import id.net.iconpln.bb.client.model.BeanModelMasterBb;
import id.net.iconpln.bb.client.model.Stock;
import id.net.iconpln.bb.client.model.Waktu;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;  
import com.extjs.gxt.ui.client.Style.Orientation;  
import com.extjs.gxt.ui.client.Style.SelectionMode;  
import com.extjs.gxt.ui.client.binding.FormBinding;  
import com.extjs.gxt.ui.client.data.ModelData;  
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;  
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;  
import com.extjs.gxt.ui.client.event.Listener;  
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;  
import com.extjs.gxt.ui.client.event.SelectionListener;  
import com.extjs.gxt.ui.client.store.ListStore;  
import com.extjs.gxt.ui.client.store.Store;  
import com.extjs.gxt.ui.client.widget.ContentPanel;  
import com.extjs.gxt.ui.client.widget.LayoutContainer;  
import com.extjs.gxt.ui.client.widget.button.Button;  
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;  
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;  
import com.extjs.gxt.ui.client.widget.form.NumberField;  
import com.extjs.gxt.ui.client.widget.form.TextField;  
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;  
import com.extjs.gxt.ui.client.widget.grid.ColumnData;  
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;  
import com.extjs.gxt.ui.client.widget.grid.Grid;  
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;  
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;  
import com.extjs.gxt.ui.client.widget.layout.RowLayout;  
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;  
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;  
import com.google.gwt.i18n.client.NumberFormat;  
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbKebutuhanThn extends LayoutContainer {
	private final BatubaraServiceAsync Service = GWT.create(BatubaraService.class);
	private FormBinding formBindings;  
	private FormData formData; 
	private Grid<Stock> grid;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
//	    setStyleAttribute("margin", "5px");
	    
	    setLayout(new FitLayout());
	    
	    formData = new FormData("-20"); 
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Perencanaan Kebutuhan Batubara tahunan");  
	    cp.setFrame(true);  
	    cp.setSize(900, 400);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();  
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	        new Listener<SelectionChangedEvent<Stock>>() {  
	          public void handleEvent(SelectionChangedEvent<Stock> be) {  
	            if (be.getSelection().size() > 0) {  
	              formBindings.bind((ModelData) be.getSelection().get(0));  
	            } else {  
	              formBindings.unbind();  
	            }  
	          }  
	        });  
	      
	  
	    FormPanel panel = createForm();  
	    formBindings = new FormBinding(panel, true);  
	    formBindings.setStore((Store) grid.getStore());  
	  
	    cp.add(panel, new RowData(.4, 1));  	  	    
	    cp.add(grid, new RowData(.6, 1));
//		setScrollMode(Scroll.AUTO);		
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();	    
	    panel.setHeaderVisible(false);
	    
	    final TextField<String> change = new TextField<String>();
	  	  
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("Produksi Daya");  
	    fieldSet.setCollapsible(false);
	    
	  
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(75);  
	    fieldSet.setLayout(layout);  
	  	
	    List<Waktu> stocks = TestData.getTahuns();  
	    Collections.sort(stocks, new Comparator<Waktu>() {  
	      public int compare(Waktu arg0, Waktu arg1) {  
	        return arg0.getTahun().compareTo(arg1.getTahun());  
	      }  
	    });  
	    
	    ListStore<Waktu> store = new ListStore<Waktu>();  
	    store.add(stocks);  
	  
	    final ComboBox<Waktu> nama = new ComboBox<Waktu>();  
	    nama.setFieldLabel("Tahun");  
	    nama.setDisplayField("tahun");
//	    combo.setWidth(width)
	    nama.setTriggerAction(TriggerAction.ALL);  
	    nama.setStore(store);  
	    fieldSet.add(nama, formData);  
	    
	    nama.addListener(Events.OnChange , new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				// TODO Auto-generated method stub
				GWT.log("tahun " +nama.getRawValue());
					change.setValue(nama.getRawValue());
			}
	    	
		});
	    
//	    TextField<String> name = new TextField<String>();  
//	    name.setName("name");  
//	    name.setFieldLabel("Tahun");  
////	    panel.add(name);  
//	    fieldSet.add(name, formData);	  	     
	    
	    NumberField open = new NumberField();  
	    open.setName("daya");  
	    open.setFieldLabel("Daya (MW)");	    
//	    panel.add(open);
	    fieldSet.add(open, formData);
	    
	    TextField<String> symbol = new TextField<String>();	    
	    symbol.setName("nosk");  
	    symbol.setFieldLabel("No SK");  
//	    panel.add(symbol);  
	    fieldSet.add(symbol, formData);
	  	    	 
	    panel.add(fieldSet);  
	    
  
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Kebutuan Batubara");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(95);  
	    fieldSet2.setLayout(layout2);  

	    
	      
	    change.setName("tahun");  
	    change.setFieldLabel("Tahun");  
//	    change.setFormat(NumberFormat.getDecimalFormat());  
//	    panel.add(change);
	    fieldSet2.add(change, formData);
	  
	    NumberField open2 = new NumberField();  
	    open2.setName("batubara");  
	    open2.setFieldLabel("BatuBara (TON)");	    
//	    panel.add(open);
	    fieldSet2.add(open2, formData);
	    
	    TextField<String> symbol2 = new TextField<String>();	    
	    symbol2.setName("nosk");  
	    symbol2.setFieldLabel("No SK");  
//	    panel.add(symbol);  
	    fieldSet2.add(symbol2, formData);	    	    	   
	    
	    DateField last = new DateField();  
	    last.setName("date");  
	    last.setFieldLabel("Updated date");  
//	    panel.add(last);	    
	    fieldSet2.add(last, formData);
	  
	    panel.add(fieldSet2);

	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();  
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();  
//	        getMasterBbUraian();
	      }  
	    }));  

	    panel.add(p);
	    
	    return panel;  
	  }  
	  
	  private Grid<Stock> createGrid() {  
	    final NumberFormat currency = NumberFormat.getCurrencyFormat();  
	    final NumberFormat number = NumberFormat.getFormat("0.00");  
	    final NumberCellRenderer<Grid<Stock>> numberRenderer = new NumberCellRenderer<Grid<Stock>>(  
	        currency);  
	  
	    GridCellRenderer<Stock> change = new GridCellRenderer<Stock>() {  
	  
	      public String render(Stock model, String property, ColumnData config, int rowIndex,  
	          int colIndex, ListStore<Stock> store, Grid<Stock> grid) {  
	        double val = (Double) model.get(property);  
	        String style = val < 0 ? "red" : "green";  
	        return "<span style='color:" + style + "'>" + number.format(val) + "</span>";  
	      }  
	    };  
	  
	    GridCellRenderer<Stock> gridNumber = new GridCellRenderer<Stock>() {  
	      public String render(Stock model, String property, ColumnData config, int rowIndex,  
	          int colIndex, ListStore<Stock> store, Grid<Stock> grid) {  
	        return numberRenderer.render(null, property, model.get(property));  
	      }  
	    };  
	  
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

	    ColumnConfig column = new ColumnConfig();
	    
	    column = new ColumnConfig();  
	    column.setId("nosk");  
	    column.setHeader("No SK");  
	    column.setAlignment(HorizontalAlignment.LEFT);  
	    column.setWidth(150);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);  

	    
	    column = new ColumnConfig();  
	    column.setId("tahun");  
	    column.setHeader("Tahun");  
	    column.setWidth(90);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("daya");  
	    column.setHeader("Daya (MW)");  
	    column.setWidth(150);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("batubara");  
	    column.setHeader("Batubara (TON)");  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setWidth(150);  
//	    column.setRenderer(gridNumber);  
	    configs.add(column);  
	  
//	    column = new ColumnConfig("change", "Change", 90);  
//	    column.setAlignment(HorizontalAlignment.RIGHT);  
//	    column.setRenderer(change);  
//	    configs.add(column);  
	    
	  
	    column = new ColumnConfig("date", "Last Updated", 90);  
	    column.setAlignment(HorizontalAlignment.RIGHT);  
	    column.setDateTimeFormat(DateTimeFormat.getShortDateFormat());  
	    configs.add(column);  
	  
	    ListStore<Stock> store = new ListStore<Stock>();  
	    store.setMonitorChanges(true);  
//	    store.add(TestData.getStocks());
	    store.add(TestData.getKebutuhan());
	  
	    ColumnModel cm = new ColumnModel(configs);  
	  
	    Grid<Stock> grid = new Grid<Stock>(store, cm);  
	    grid.getView().setEmptyText("sdfd");  
	    grid.setBorders(false);  
	    grid.setAutoExpandColumn("tahun");  
	    grid.setBorders(true);  
	  
	    return grid;  
	  }  
	  
	  private void getMasterBbUraian(){
		  Service.getListMasterBb("URAIAN",new AsyncCallback<List<BeanModelMasterBb>>() {
						public void onFailure(Throwable caught) {
							GWT.log(caught.getMessage(), null);
						}

						@Override
						public void onSuccess(List<BeanModelMasterBb> result) {
							// TODO Auto-generated method stub
							GWT.log("total data " + result.size());
						}

		  });					
	  }  
}
